import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, Request } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { MemoryService } from './memory.service';
import { CreateMemoryDto, UpdateMemoryDto, MemoryQueryDto } from './dto/memory.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@ApiTags('Memory')
@Controller('memory')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class MemoryController {
  constructor(private readonly memoryService: MemoryService) {}

  // Create memory
  @Post()
  @ApiOperation({ summary: 'Create memory' })
  @ApiResponse({ status: 201, description: 'Created successfully' })
  @ApiResponse({ status: 400, description: 'Invalid request parameters' })
  async createMemory(@Request() req, @Body() createMemoryDto: CreateMemoryDto) {
    return this.memoryService.createMemory(req.user.id, createMemoryDto);
  }

  // Get memory list
  @Get()
  @ApiOperation({ summary: 'Get memory list' })
  @ApiResponse({ status: 200, description: 'Retrieved successfully' })
  async getMemories(@Request() req, @Query() query: MemoryQueryDto) {
    return this.memoryService.getMemories(req.user.id, query);
  }

  // Search memories
  @Get('search')
  @ApiOperation({ summary: 'Search memories' })
  @ApiResponse({ status: 200, description: 'Search successful' })
  async searchMemories(@Request() req, @Query('q') query: string, @Query('limit') limit?: number) {
    return this.memoryService.searchMemories(req.user.id, query, limit);
  }

  // Get memory statistics
  @Get('stats')
  @ApiOperation({ summary: 'Get memory statistics' })
  @ApiResponse({ status: 200, description: 'Retrieved successfully' })
  async getMemoryStats(@Request() req) {
    return this.memoryService.getMemoryStats(req.user.id);
  }

  // Update memory
  @Put(':id')
  @ApiOperation({ summary: 'Update memory' })
  @ApiResponse({ status: 200, description: 'Updated successfully' })
  @ApiResponse({ status: 404, description: 'Memory not found' })
  async updateMemory(@Request() req, @Param('id') id: string, @Body() updateMemoryDto: UpdateMemoryDto) {
    return this.memoryService.updateMemory(req.user.id, id, updateMemoryDto);
  }

  // Delete memory
  @Delete(':id')
  @ApiOperation({ summary: 'Delete memory' })
  @ApiResponse({ status: 200, description: 'Deleted successfully' })
  @ApiResponse({ status: 404, description: 'Memory not found' })
  async deleteMemory(@Request() req, @Param('id') id: string) {
    return this.memoryService.deleteMemory(req.user.id, id);
  }
}
